package com.shuyu.web.controller.api;

import com.alibaba.fastjson2.JSONObject;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.shuyu.common.core.controller.BaseController;
import com.shuyu.service.PayNotifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/notify")
@Api(tags = "支付回调相关")
public class PayNotifyController extends BaseController {
//    @Resource
//    private WxPayService wxService;
    @Resource
    private PayNotifyService payNotifyService;
    @PostMapping("/wx")
    @ApiOperation("微信支付成功数据回调")
    public String wx(@RequestBody String xmlData) throws WxPayException {
        WxPayService wxService = new WxPayServiceImpl();
        final WxPayOrderNotifyResult notifyResult = wxService.parseOrderNotifyResult(xmlData);
        payNotifyService.handleOrderNotifyResult(notifyResult);
        return WxPayNotifyResponse.success("成功");
    }
    @PostMapping("/alipay")
    @ApiOperation("支付宝支付成功数据回调")
    public String alipay(HttpServletRequest request){
        Map<String, String[]> requestParams = request.getParameterMap();
        System.out.println(requestParams);
        JSONObject json = new JSONObject();
        json.put("request",requestParams);
        System.out.println(json);
        String outTradeNo = request.getParameter("out_trade_no");//商户支付订单号
        String tradeStatus = request.getParameter("trade_status");//交易结果码
        String tradeNo = request.getParameter("trade_no"); // 支付宝交易号
        payNotifyService.handleAlipayOrderNotifyResult(outTradeNo, tradeStatus, tradeNo);
        return "success";
    }

}
